package com.xxl.job.executor.service.impl;

import com.xxl.job.core.log.XxlJobLogger;
import com.xxl.job.executor.common.util.DBUtils;
import com.xxl.job.executor.common.util.SpringContextHolder;
import com.xxl.job.executor.service.IGrantryTransService;
import org.springframework.jdbc.datasource.DataSourceUtils;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.sql.Connection;

@Service("grantryTransService")
public class GrantryTransService implements IGrantryTransService {
    Connection connPre = null;

    @Override
    public void transfer() throws Exception {
        XxlJobLogger.log("=====开始执行业务操作===");
        final String recordName = "grantry_trans_main";
        try {
            DataSource dataSourcePre = SpringContextHolder.getBean("dataSourcePre");
            connPre = DataSourceUtils.getConnection(dataSourcePre);
            connPre.setAutoCommit(false);

            String recordStr = DBUtils.getCurSynRecordNum(connPre, recordName);
            XxlJobLogger.log("[普通]本次最大记录RECORDID为:" + recordStr);

            String dbCurTime = DBUtils.getDBCurTime(connPre);
            XxlJobLogger.log("[普通]本次同步截止时间为:" + dbCurTime);

            StringBuffer sql = new StringBuffer();
            sql.append("insert into TB_SYNC_GBUPLOAD_ETCTU(id,mediatype,mediano,obusign,cardid,etccardtype,vehicleid,vehicletype,vehicleclass," +
                    "entolllaneid,entolllanehex,entime,passid,vehiclesign,stationno,tollgantryid," +
                    "gantrytype,gantryhex,payfee,fee,discountfee,transtime,transfee,hourbatchno," +
                    "transferstatus,receivetime,recordid,inserttime,batchno) " +
                    "select tradeId id,mediatype,(case when mediatype in (1,2) then OBUSN else null end) as mediano,obusign,CPUCardId cardid,etccardtype,vehiclePlate vehicleid,vehicletype,vehicleclass," +
                    "entolllaneid,null entolllanehex,entime,passid,vehiclesign,(select STATION_ID from tb_charge_grantry_station b where a.gantryId=b.GRANTRY_ID and rownum=1) stationno,gantryId tollgantryid," +
                    "gantrytype,gantryhex,payfee,fee,discountfee,transtime,transfee,hourbatchno," +
                    "1 transferstatus,sysdate receivetime,SEQ_TB_SYNC_GBUPLOAD_ETCTU.nextval recordid,sysdate inserttime,null batchno " +
                    "from grantry_trans_main a " +
                    "where inserttabletime>=to_date('" + recordStr + "','yyyy-mm-dd hh24:mi:ss') and inserttabletime<to_date('" + dbCurTime + "','yyyy-mm-dd hh24:mi:ss') ");
            Integer count = DBUtils.execute(connPre, sql.toString());
            XxlJobLogger.log("[普通]本次从中台系统共同步(条):" + count);

            count = DBUtils.modifySynRecordNum(connPre, recordName, recordStr, dbCurTime);
            if (count == null || count != 1) {
                XxlJobLogger.log("[异常][普通]更新记录时间异常");
                throw new Exception("[普通]更新记录时间异常");
            }
            XxlJobLogger.log("[普通]更新记录时间为:" + dbCurTime);

            connPre.commit();
            XxlJobLogger.log("=====完成执行业务操作===");
        }catch(Exception e){
            if (connPre != null) {
                try {
                    connPre.rollback();
                    XxlJobLogger.log("回滚成功.");
                }catch (Exception e1){
                    e1.printStackTrace();
                }
            }
            XxlJobLogger.log("抛出异常："+e.getMessage());
            throw e;
        }finally{
            if (connPre != null) {
                try {
                    connPre.setAutoCommit(true);
                    connPre.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
    }
}
